#include "view_distances.h"
#include "app_trace.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "common/common.h"
#include "datetime.h"
#include "display_ctrl.h"
#include "display_interface.h"
#include "font/font.h"
#include "font_mngr.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "math.h"
#include "platform_assert.h"
#include "rtc.h"
#include "sport_his.h"
#include "view_distances.inc"
#include "weather_ctrl.h"
#define P_MILEAGE_NUM_Y_POS 108

void view_distances_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[0] = sport_get_distance() * 0.6213712;
            disp_ctrl.com_data.i_temp[1] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[0] = sport_get_distance();
            disp_ctrl.com_data.i_temp[1] = 1;
        }
    }
    int num = disp_ctrl.com_data.i_temp[0];
    draw_on_chip_gradients_image(6, 8, zip_image_circle, 0x87e7, 0x3d80);
    draw_on_chip_gray_gif_array_filter_color(6, 8, zip_gif_mileage, sizeof(zip_gif_mileage) / sizeof(zip_gif_mileage[0]), 0xFFFF);
    if (disp_ctrl.com_data.i_temp[1] == 1)
    {
        draw_on_chip_gray_image(32, 142, zip_image_km, 0xFFFF);
    }
    else
    {
        draw_on_chip_gray_image(32, 142, zip_image_mi, 0xFFFF);
    }
    uint8_t num_buf[4] = {0};
    num_buf[0] = num / 10000 % 10; // 万步
    num_buf[1] = num / 1000 % 10;  // 千步
    num_buf[2] = num / 100 % 10;   // 百步
    num_buf[3] = num / 10 % 10;    // 十步
    uint8_t offset;
    if (num < 10000)
    {
        offset = 19;
        /* 1.01 */
        draw_on_chip_gray_image(offset, P_MILEAGE_NUM_Y_POS, zip_curve_num_10x28[num_buf[1]], RGB565_COLOR_WHITE);
        draw_on_chip_gray_image(offset + 11, P_MILEAGE_NUM_Y_POS, zip_image_dot, RGB565_COLOR_WHITE);
        draw_on_chip_gray_image(offset + 21, P_MILEAGE_NUM_Y_POS, zip_curve_num_10x28[num_buf[2]], RGB565_COLOR_WHITE);
        draw_on_chip_gray_image(offset + 32, P_MILEAGE_NUM_Y_POS, zip_curve_num_10x28[num_buf[3]], RGB565_COLOR_WHITE);
    }
    else
    {
        offset = 13;
        /* 11.01 */
        draw_on_chip_gray_image(offset, P_MILEAGE_NUM_Y_POS, zip_curve_num_10x28[num_buf[0]], RGB565_COLOR_WHITE);
        draw_on_chip_gray_image(offset + 11, P_MILEAGE_NUM_Y_POS, zip_curve_num_10x28[num_buf[1]], RGB565_COLOR_WHITE);
        draw_on_chip_gray_image(offset + 21, P_MILEAGE_NUM_Y_POS, zip_image_dot, RGB565_COLOR_WHITE);
        draw_on_chip_gray_image(offset + 32, P_MILEAGE_NUM_Y_POS, zip_curve_num_10x28[num_buf[2]], RGB565_COLOR_WHITE);
        draw_on_chip_gray_image(offset + 43, P_MILEAGE_NUM_Y_POS, zip_curve_num_10x28[num_buf[3]], RGB565_COLOR_WHITE);
    }
}
view_event_result_t view_distances_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
